﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountTypeRepositoryTest.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the AccountTypeRepositoryTest type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Tests
{
    using System.Linq;
    using Entities;
    using Interfaces;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.Tests.AccountTypeRepositoryTest"/> type.
    /// </summary>
    [TestClass]
    public class AccountTypeRepositoryTest
    {
        #region AccountType Tests

        #region Query Tests

        /// <summary>
        /// AccountTypes the data manager get AccountTypes test with empty repository.
        /// </summary>
        [TestMethod]
        public void AccountTypeDataManagerQueryShouldReturnEmptyListWithEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<AccountType>();
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.AccountTypes).Returns(mockObjectSet);
            var dao = new AccountTypeRepository(mockRepository.Object);
            var result = dao.Query(new AccountType(), null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count());
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// AccountTypes the data manager get AccountTypes test with non empty repository.
        /// </summary>
        [TestMethod]
        public void AccountTypeDataManagerQueryShouldReturnNonEmptyListWithNonEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<AccountType>();
            mockObjectSet.AddObject(new AccountType { Id = 1, Name = "AccountType 1" });
            mockObjectSet.AddObject(new AccountType { Id = 2, Name = "AccountType 2" });
            mockObjectSet.AddObject(new AccountType { Id = 3, Name = "AccountType 3" });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.AccountTypes).Returns(mockObjectSet);
            var dao = new AccountTypeRepository(mockRepository.Object);
            var result = dao.Query(new AccountType(), null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
            Assert.AreEqual(1, result.ElementAt(0).Id);
            Assert.AreEqual("AccountType 1", result.ElementAt(0).Name);
            Assert.AreEqual(2, result.ElementAt(1).Id);
            Assert.AreEqual("AccountType 2", result.ElementAt(1).Name);
            Assert.AreEqual(3, result.ElementAt(2).Id);
            Assert.AreEqual("AccountType 3", result.ElementAt(2).Name);
            mockRepository.VerifyAll();
        }

        #endregion Query Tests

        #region Filter Tests

        /// <summary>
        /// AccountTypes the data manager get AccountTypes test with empty repository.
        /// </summary>
        [TestMethod]
        public void AccountTypeDataManagerFilterShouldReturnEmptyListWithEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<AccountType>();
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.AccountTypes).Returns(mockObjectSet);
            var dao = new AccountTypeRepository(mockRepository.Object);
            var result = dao.Filter(x => true, null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count());
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// AccountTypes the data manager get AccountTypes test with non empty repository.
        /// </summary>
        [TestMethod]
        public void AccountTypeDataManagerFilterShouldReturnNonEmptyListWithNonEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<AccountType>();
            mockObjectSet.AddObject(new AccountType { Id = 1, Name = "AccountType 1" });
            mockObjectSet.AddObject(new AccountType { Id = 2, Name = "AccountType 2" });
            mockObjectSet.AddObject(new AccountType { Id = 3, Name = "AccountType 3" });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.AccountTypes).Returns(mockObjectSet);
            var dao = new AccountTypeRepository(mockRepository.Object);
            var result = dao.Filter(x => true, null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
            Assert.AreEqual(1, result.ElementAt(0).Id);
            Assert.AreEqual("AccountType 1", result.ElementAt(0).Name);
            Assert.AreEqual(2, result.ElementAt(1).Id);
            Assert.AreEqual("AccountType 2", result.ElementAt(1).Name);
            Assert.AreEqual(3, result.ElementAt(2).Id);
            Assert.AreEqual("AccountType 3", result.ElementAt(2).Name);
            mockRepository.VerifyAll();
        }

        #endregion Filter Tests

        #region GetById Tests

        /// <summary>
        /// Get by id test with empty repository
        /// </summary>
        [TestMethod]
        public void AccountTypeDataManagerFindByIdShouldReturnNullWithEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<AccountType>();
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.AccountTypes).Returns(mockObjectSet);
            var dao = new AccountTypeRepository(mockRepository.Object);
            var result = dao.FindById(1);
            Assert.IsNull(result);
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// Get by id test with repository with records
        /// </summary>
        [TestMethod]
        public void AccountTypeDataManagerFindByIdShouldReturnMatchingAccountType()
        {
            var mockObjectSet = new MockObjectSet<AccountType>();
            mockObjectSet.AddObject(new AccountType { Id = 1, Name = "AccountType 1" });
            mockObjectSet.AddObject(new AccountType { Id = 2, Name = "AccountType 2" });
            mockObjectSet.AddObject(new AccountType { Id = 3, Name = "AccountType 3" });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.AccountTypes).Returns(mockObjectSet);
            var dao = new AccountTypeRepository(mockRepository.Object);
            var result = dao.FindById(2);
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Id);
            Assert.AreEqual("AccountType 2", result.Name);
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// AccountTypes the data manager find by id should return null with no matching AccountType.
        /// </summary>
        [TestMethod]
        public void AccountTypeDataManagerFindByIdShouldReturnNullWithNoMatchingAccountType()
        {
            var mockObjectSet = new MockObjectSet<AccountType>();
            mockObjectSet.AddObject(new AccountType { Id = 1, Name = "AccountType 1" });
            mockObjectSet.AddObject(new AccountType { Id = 2, Name = "AccountType 2" });
            mockObjectSet.AddObject(new AccountType { Id = 3, Name = "AccountType 3" });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.AccountTypes).Returns(mockObjectSet);
            var dao = new AccountTypeRepository(mockRepository.Object);
            var result = dao.FindById(20);
            Assert.IsNull(result);
            mockRepository.VerifyAll();
        }

        #endregion GetById Tests

        #endregion AccountType Tests
    }
}